package com.menggudf.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.menggudf.commom.CommonUtils;
import com.menggudf.dao.UserInfoDao;
import com.menggudf.entity.UserInfo;
import com.menggudf.model.ResultModel;
import com.menggudf.model.UserInfoModel;
import com.menggudf.service.UserInfoService;

@Service
public class UserInfoServiceImpl implements UserInfoService{

    @Autowired
    private UserInfoDao userInfoDao;
    
    @Override
    @Transactional(value = "medicalTM", rollbackFor = Exception.class)
    public ResultModel updateUserInfo(UserInfoModel userInfo) {
        UserInfo info = new UserInfo();
        if(CommonUtils.isEmpty(userInfo.getAbdominalLine())){
            info.setAbdominalLine(userInfo.getAbdominalLine());
        }
        if(CommonUtils.isEmpty(userInfo.getBloodPressure())){
            info.setBloodPressure(userInfo.getBloodPressure());
        }
        if(CommonUtils.isEmpty(userInfo.getHeight())){
            info.setHeight(userInfo.getHeight());
        }
        if(CommonUtils.isEmpty(userInfo.getHipLine())){
            info.setHipLine(userInfo.getHipLine());
        }
        if(CommonUtils.isEmpty(userInfo.getPulse())){
            info.setPulse(userInfo.getPulse());
        }
        if(CommonUtils.isEmpty(userInfo.getWeight())){
            info.setWeight(userInfo.getWeight());
        }
        userInfoDao.persist(info);
        return ResultModel.success();
    }

    @Override
    @Transactional(value = "medicalTM", readOnly=true)
    public Map<String, Object> getUserInfoList(String userCode) {
        List<UserInfo> list = userInfoDao.getUserInfoList(userCode);
        List<UserInfoModel> mlist = new ArrayList<UserInfoModel>();
        Map<String, Object> map = new HashMap<String, Object>();
        for(UserInfo u:list){
            UserInfoModel um = new UserInfoModel();
            um.setAbdominalLine(u.getAbdominalLine());
            um.setBloodPressure(u.getBloodPressure());
            um.setHeight(u.getHeight());
            um.setHipLine(u.getHipLine());
            um.setPulse(u.getPulse());
            um.setWeight(u.getWeight());
            um.setCreateTime(u.getCreateTime());
            mlist.add(um);
        }
        map.put("total", mlist.size());
        map.put("rows", mlist);
        return map;
    }
}
